import { Message, Loading } from "ccui";
import { downloadAttachment } from '@/basa/api'

export const loadingParams = { isShowLoading: true };
export const pageLoadingParams = { isShowPageLoading: true };

export function showErrorMessage(message) {
  return Message({
    message,
    type: "error",
    showClose: true,
    ccuiCustom: true,
    duration: 1000 * 8
  });
}

export function showLoading(loadingText = "加载中") {
  return Loading.service({
    lock: true,
    text: `${loadingText}...`,
    spinner: "el-icon-loading",
    background: "rgba(0, 0, 0, 0.2)",
    customClass: "ccui-custom-loading"
  });
}

// 所有导出皆可用
export async function exportData(res) {
  let tempStorage = res;
  let openUrl = document.createElement("a");
  openUrl.style.display = "none";

  // 不是流的导出需要调接口查所对应的文件流
  if (tempStorage.ossUrl) {
    const requestData = { downloadUrl: tempStorage.ossUrl, fileName: tempStorage.fileName }
    tempStorage = await downloadAttachment(requestData);
  }
  // 如果本来是文件流的话则直接导出
  const url = URL.createObjectURL(
    new Blob([tempStorage.data], { type: tempStorage.data.type })
  );
  // 设置文件名  
  openUrl.download = decodeURIComponent(tempStorage.headers['content-disposition'].split('=utf-8\'\'')[1], "UTF-8")

  openUrl.href = url;
  openUrl.setAttribute("rel", "noopener noreferrer");
  document.body.appendChild(openUrl);
  openUrl.click();
  document.body.removeChild(openUrl);
  URL.revokeObjectURL(url); // 释放内存
}

export function dropConsole() {
  console.error = function () { };
  console.log = function () { };
  console.info = function () { };
}
